package com.tbr.blockchain.controller;

import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xpath.internal.SourceTree;
import com.tbr.blockchain.block.BlockService;
import com.tbr.blockchain.model.Block;
import com.tbr.blockchain.model.Transaction;
import com.tbr.blockchain.model.TransactionParam;
import com.tbr.blockchain.model.Wallet;
import com.tbr.blockchain.p2p.Message;
import com.tbr.blockchain.p2p.P2PClient;
import com.tbr.blockchain.p2p.P2PServer;
import com.tbr.blockchain.p2p.P2PService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


@RestController("/btc")
public class BlockController {
    @Autowired
    P2PService p2PService;

    @Autowired
    BlockService blockService;

    @GetMapping("/create")
    public String createWallet(){
        System.out.println("创建钱包");
        Wallet wallet=blockService.createWallet();
        Wallet[] wallets={new Wallet(wallet.getPublicKey())};
        String msg=JSON.toJSONString(new Message(P2PService.RESPONSE_WALLET,JSON.toJSONString(wallets)));
        p2PService.broatcast(msg);
        return JSON.toJSONString(wallet);
    }
    @GetMapping("/allwallet")
    public Map<String,Wallet> showMyWallet(){
        return blockService.getMyWalletMap();
    }

   @GetMapping("/mine/{address}")
    public Block mineIng(@PathVariable("address") String address){
       Block mine = blockService.mine(address);
       Block[] blocks={mine};
       String msg= JSON.toJSONString(new Message(P2PService.RESPONSE_BLOCKCHAIN,JSON.toJSONString(blocks)));
       p2PService.broatcast(msg);
       return mine;
   }

   @GetMapping("/otherWallet")
   public Map<String,Wallet> showOtherWallet(){
        return blockService.getOtherWalletMap();
   }
   @GetMapping("/allblock")
    public List<Block> showAllBlock(){
        return blockService.getBlocksChain();
   }

   @GetMapping("/money/{address}")
   public int BTCamount(@PathVariable("address") String address){
        return blockService.getWalletBalance(address);
   }

   @PostMapping("/transaction")
    public Transaction tx(@RequestBody TransactionParam tp){
        Map<String,Wallet> MyWallet=blockService.getMyWalletMap();
       Set<String> keys = MyWallet.keySet();
       Wallet sender=new Wallet(),recipient=new Wallet();
       for(String key: keys){
           if(tp.getSender().equals(key)){
               sender=MyWallet.get(key);
           }
       }
       Map<String,Wallet> otherWallet=blockService.getOtherWalletMap();
       Set<String> others= otherWallet.keySet();
       for(String key: others){
           if(tp.getRecipient().equals(key)){
               recipient=otherWallet.get(key);
           }
       }
       Transaction tx = blockService.createTransaction(sender, recipient, tp.getAmount());
       System.out.println(JSON.toJSONString(tx));
       String msg1= JSON.toJSONString(new Message(P2PService.RESPONSE_TRANSACTION,JSON.toJSONString(tx)));
       p2PService.broatcast(msg1);
       return tx;
   }

   @GetMapping("/packTransacation")
    public  List<Transaction> ptx(){
       List<Transaction> blockTxs=new ArrayList<Transaction>(blockService.getAllTransaction());
       blockTxs.removeAll(blockService.getPackedTransaction());
       return blockTxs;
   }

   @GetMapping("/alltransaction")
    public List<Transaction> atx(){
        return blockService.getAllTransaction();
   }





}
